package cn.iocoder.boot.controller.admin;


import cn.iocoder.boot.dto.OrderTo;
import cn.iocoder.boot.dto.QueryTO;
import cn.iocoder.boot.entity.ComponentPrice;
import cn.iocoder.boot.entity.ComponentStock;
import cn.iocoder.boot.service.CallLimitService;
import cn.iocoder.boot.service.QuoteService;
import cn.iocoder.boot.service.impl.supplier.JBCHIPServiceImpl;
import cn.iocoder.boot.service.impl.supplier.YunhanServiceImpl;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.config.SecurityProperties;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.api.oauth2.OAuth2TokenApi;
import cn.iocoder.yudao.module.system.api.oauth2.dto.OAuth2AccessTokenCheckRespDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.boot.enums.SupplierCodeEnum.IC;
import static cn.iocoder.boot.enums.SupplierCodeEnum.JBCHIP;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getRequest;

@Tag(name = "询价")
@RestController
@RequestMapping("/quote")
public class QuoteController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private YunhanServiceImpl yunhanService;

    @Autowired
    private JBCHIPServiceImpl jbchipService;
    @Resource
    private SecurityProperties securityProperties;
    @Resource
    private OAuth2TokenApi oauth2TokenApi;

    @Resource
    private CallLimitService callLimitService;


    @Resource
    private QuoteService service;

    @PostMapping("/query")
    @Operation(summary = "询价总入口分页")
    @PermitAll
    public CommonResult<Page<ComponentStock>> query(@RequestBody QueryTO to) throws Exception {


        Page<ComponentStock> query = service.query(to);
        return success(query);
    }

    @PostMapping("/queryList")
    @Operation(summary = "询价总入口列表")
    public CommonResult<List<ComponentPrice>> queryList(@RequestBody QueryTO to) throws Exception {
        HttpServletRequest request = getRequest();
        assert request != null;
        String token = SecurityFrameworkUtils.obtainAuthorization(request,
                securityProperties.getTokenHeader(), securityProperties.getTokenParameter());

        OAuth2AccessTokenCheckRespDTO accessToken = oauth2TokenApi.checkAccessToken(token);
        if (!callLimitService.canCall(accessToken.getUserId())) {
            return error(500, "您已超出每日调用限制，请明天再试！");
        }

        List<ComponentPrice> list = service.queryList(to);
        if (list == null) {
            return success(new ArrayList<>());
        }
        return success(list);
    }


    @PostMapping("/queryBaseInfo")
    @Operation(summary = "查询型号基本信息")
    public CommonResult<IPage<ComponentStock>> queryBaseInfo(@RequestBody QueryTO to) {
        return success(service.queryBaseInfo(to));
    }

    //下单总入口
    @PostMapping("/order")
    @Operation(summary = "下单总入口")
    @ResponseBody
    @PermitAll
    public CommonResult order(@RequestBody OrderTo to) {

        if (StringUtils.isNotBlank(to.getSupplier())) {
            if (to.getSupplier().equals(IC.getCode())) {
                return yunhanService.order(to);
            } else if (to.getSupplier().equals(JBCHIP.getCode())) {
                return jbchipService.order(to);

            }
        }
        return error(500, "请指定供应商");
    }


    @PostMapping("/forRemote/queryList")
    @Operation(summary = "远程询价总入口列表")
    public CommonResult<List<ComponentPrice>> queryListOfRemote(@RequestBody QueryTO to) throws Exception {
        HttpServletRequest request = getRequest();
        String token = null;
        if (request != null) {
            token = SecurityFrameworkUtils.obtainAuthorization(request,
                    securityProperties.getTokenHeader(), securityProperties.getTokenParameter());
        }
        OAuth2AccessTokenCheckRespDTO accessToken = oauth2TokenApi.checkAccessToken(token);
        if (!callLimitService.canCall(accessToken.getUserId())) {
            return error(500, "您已超出每日调用限制，请明天再试！");
        }
        List<ComponentPrice> list = service.queryListOfRemote(to);
        if (list == null) {
            return success(new ArrayList<>());
        }
        return success(list);
    }

}
